package com.xin;

import org.apache.commons.lang3.StringUtils;

// 将字符串中的空格替换为20%,下面我将用两种方法实现
public class ReplaceSpace {
    public static void main(String[] args) {
        replaceSpace1("haha haha haha");
    }

    private static void replaceSpace(String str) {
        String str1 = StringUtils.replace(str, " ", "20%");

        System.out.println(str1);
    }

    private static void replaceSpace1(String str) {

        char[] chars = str.toCharArray();
        char[] charsNew = new char[chars.length * 3];
        int size = 0;
        for (int i =0; i < str.length(); i++) {
            if (chars[i] == ' ') {
                charsNew[size++] = '2';
                charsNew[size++] = '0';
                charsNew[size++] = '%';
            } else {
                charsNew[size++] = chars[i];
            }
        }

        System.out.println(charsNew);
    }
}
